﻿using OnHook.Business.IServices.Auth;
using OnHook.Business.IServices.Common;
using OnHook.Business.Model;
using OnHook.Business.Services.Auth;
using OnHook.Business.Vo.UserInfo;
using OnHook.Domain.Enums;
using OnHook.Framework.Api;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using NPOI.OpenXmlFormats.Wordprocessing;
using Serilog;
using SKIT.FlurlHttpClient.Wechat.Api;

namespace OnHook.WebApi.Controllers
{
    /// <summary>
    /// 第三方授权
    /// </summary>
    [Route("open")]
    public class OpenAuthController(IAuthService authService,IJwtService jwtService,IOpenAuthService wechatAuthService) : Controller
    {
        private readonly IAuthService _authService = authService;
        private readonly IJwtService _jwtService = jwtService;
        private readonly IOpenAuthService _wechatAuthService = wechatAuthService;

        /// <summary>
        /// 授权
        /// </summary>
        /// <param name="platform">那种平台</param>
        /// <param name="authCallBackUrl">授权回调地址-前端页面获取code和state参数的地址</param>
        /// <param name="returnUrl">授权成功以后跳转地址</param>
        /// <param name="invitecode">邀请码</param>
        /// <returns></returns>
        [HttpGet("auth")]
        [AllowAnonymous]
        public async Task<IActionResult> Auth(EnumPlatform platform, string authCallBackUrl, string returnUrl = "", string invitecode = "")
        {
            return Redirect(await _authService.GetAuthUrlAsync(platform, authCallBackUrl, returnUrl, invitecode));
        }

        /// <summary>
        /// 授权回调
        /// </summary>
        /// <param name="code"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        [HttpGet("auth-callback")]
        [AllowAnonymous]
        public async Task<IActionResult> GetCallBackUrl(string code, string state)
        {
            return Redirect(await _authService.GetAuthCallBackUrlAsync(code, state));
        }

        /// <summary>
        /// 授权回调提交接口
        /// </summary>
        /// <param name="model">授权用户信息model</param>
        /// <returns></returns>
        [HttpPost("openlogin")]
        [AllowAnonymous]
        public async Task<ApiResult<LoginUserVo>> OpenLogin([FromBody] OpenUserInfoModel model)
        {
            return await ApiResult<LoginUserVo>.Successed(() => _authService.OpenLoginAsync(model));
        }

        /// <summary>
        /// 授权回调提交接口
        /// </summary>
        /// <returns></returns>
        [HttpGet("testlogin")]
        [AllowAnonymous]
        public async Task<ApiResult<LoginUserVo>> TestLogin()
        {
            return await ApiResult<LoginUserVo>.Successed(() => _authService.TestLogin());
        }

        /// <summary>
        /// 授权回调提交接口
        /// </summary>
        /// <returns></returns>
        [HttpGet("tesst")]
        public ApiResult<LoginUserVo> TestGet([FromQuery]string token)
        {
            return ApiResult<LoginUserVo>.Successed( _jwtService.GetLoginUserVoByToken(token,out _));
        }
        /// <summary>
        /// 授权回调提交接口
        /// </summary>
        /// <returns></returns>
        [HttpGet("current-user")]
        public ApiResult<LoginUserVo> GetCurrentUser()
        {
            return ApiResult<LoginUserVo>.Successed(_jwtService.GetCurrentUser()!);
        }

        /// <summary>
        /// 检查签名
        /// </summary>
        /// <param name="signature"></param>
        /// <param name="timestamp"></param>
        /// <param name="nonce"></param>
        /// <param name="echostr"></param>
        /// <returns></returns>
        [HttpGet("checksign")]
        [AllowAnonymous]
        public ActionResult CheckSign(string signature, string timestamp, string nonce, string echostr)
        {
            Log.Error($"收到来自微信的验签请求:signature={signature}&timestamp={timestamp}&nonce={nonce}&echostr={echostr}");
            return this.Content(_wechatAuthService.CheckSign(signature, timestamp, nonce, echostr));
        }

        /// <summary>
        /// 获取当前时间
        /// </summary>
        /// <returns></returns>
        [HttpGet("current-time")]
        public ApiResult<DateTime> GetCurrentTime()
        {
            return ApiResult<DateTime>.Successed(DateTime.Now);
        }
    }
}
